package 综合练习题;

import java.util.HashMap;
import java.util.Map;

// 定义 Product 类
class Product {
    private int id;
    private String name;
    private double price;

    public Product(int id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return id == product.id;
    }

    @Override
    public int hashCode() {
        return Integer.hashCode(id);
    }

    @Override
    public String toString() {
        return "Product{id=" + id + ", name='" + name + "', price=" + price + "}";
    }
}

// 模拟库存服务
class InventoryService {
    private static final Map<Product, Integer> stockMap = new HashMap<>();

    public static void setStock(Product product, int stock) {
        stockMap.put(product, stock);
    }

    public static int getStock(Product product) {
        return stockMap.getOrDefault(product, 0);
    }
}

// 购物车类
class ShoppingCart {
    private final Map<Product, Integer> cart;

    public ShoppingCart() {
        this.cart = new HashMap<>();
    }

    // 添加商品到购物车
    public void addProduct(Product product, int quantity) {
        int stock = InventoryService.getStock(product);
        if (quantity > stock) {
            throw new IllegalArgumentException("商品 " + product.getName() + " 库存不足，当前库存：" + stock);
        }
        cart.merge(product, quantity, Integer::sum);
        InventoryService.setStock(product, stock - quantity);
    }

    // 移除指定数量的商品
    public void removeProduct(Product product, int quantity) {
        if (cart.containsKey(product)) {
            int currentQuantity = cart.get(product);
            if (quantity > currentQuantity) {
                throw new IllegalArgumentException("购物车中商品 " + product.getName() + " 数量不足，当前数量：" + currentQuantity);
            }
            int newQuantity = currentQuantity - quantity;
            if (newQuantity == 0) {
                cart.remove(product);
            } else {
                cart.put(product, newQuantity);
            }
            int stock = InventoryService.getStock(product);
            InventoryService.setStock(product, stock + quantity);
        }
    }

    // 计算购物车总金额
    public double calculateTotal() {
        return cart.entrySet().stream()
               .mapToDouble(entry -> entry.getKey().getPrice() * entry.getValue())
               .sum();
    }

    // 打印购物车内容
    public void printCart() {
        System.out.println("购物车内容：");
        for (Map.Entry<Product, Integer> entry : cart.entrySet()) {
            System.out.println(entry.getKey() + "，数量：" + entry.getValue());
        }
    }
}

public class ShoppingCartApp {
    public static void main(String[] args) {
        Product product1 = new Product(1, "苹果", 5.0);
        Product product2 = new Product(2, "香蕉", 3.0);

        // 设置商品库存
        InventoryService.setStock(product1, 10);
        InventoryService.setStock(product2, 15);

        ShoppingCart cart = new ShoppingCart();

        // 添加商品到购物车
        cart.addProduct(product1, 2);
        cart.addProduct(product2, 3);
        cart.addProduct(product1, 1);
        cart.printCart();

        // 计算购物车总金额
        System.out.println("购物车总金额：" + cart.calculateTotal());

        // 移除指定数量的商品
        cart.removeProduct(product1, 2);
        cart.printCart();

        // 再次计算购物车总金额
        System.out.println("购物车总金额：" + cart.calculateTotal());
    }
}